package com.zhentao.controller;

import com.github.pagehelper.PageInfo;
import com.zhentao.annotation.IdEncrypt;
import com.zhentao.model.dto.system.AssginRoleDto;
import com.zhentao.model.dto.system.SysUserDto;
import com.zhentao.model.entity.system.SysUser;
import com.zhentao.model.vo.common.Result;
import com.zhentao.model.vo.common.ResultCodeEnum;
import com.zhentao.service.SysUserService;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "用户接口")
@RestController
@RequestMapping(value = "/system/sysUser")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    //TODO 已完成
    //分页查询用户
    @GetMapping(value = "findByPage/{pageNum}/{pageSize}")
    @IdEncrypt
    public Result<PageInfo<SysUser>> findByPage(SysUserDto sysUserDto,
                                                @PathVariable(value = "pageNum") Integer pageNum,
                                                @PathVariable(value = "pageSize") Integer pageSize){
        PageInfo<SysUser> pageInfo = sysUserService.findByPage(sysUserDto,pageNum,pageSize);
        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }

    //TODO 已完成
    //添加用户
    @PostMapping(value = "saveSysUser")
    public Result saveSysUser(@RequestBody SysUser sysUser){
        sysUserService.saveSysUser(sysUser);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    //TODO 已完成
    //修改用户
    @PutMapping(value = "updateSysUser")
    public Result updateSysUser(@RequestBody SysUser sysUser){
        sysUserService.updateSysUser(sysUser);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }


    //TODO 已完成
    //删除用户
    @DeleteMapping(value = "deleteById/{id}")
    public Result deleteSysUser(@PathVariable(value = "id") Long userId){
        sysUserService.deleteSysUser(userId);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    //TODO 已完成
    //为用户分配角色
    @PostMapping(value = "doAssign")
    public Result doAssign(@RequestBody AssginRoleDto assginRoleDto){
        sysUserService.doAssign(assginRoleDto);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

}
